#include <stdio.h>
#include <string.h>
#include <shadow.h>
#include <crypt.h>
#include <unistd.h>

#define NAMESIZE	32

int main(void)
{
	char name[NAMESIZE] = {};
	char *pwd;
	struct spwd *shadow_pwd;
	char *crypt_pwd;

	// 读用户名
	printf("username:");
	fgets(name, NAMESIZE, stdin);
	// 覆盖掉输入的\n
	*strchr(name, '\n') = '\0';

	// 读密码
	pwd = getpass("password:");
	
	// 校验
	shadow_pwd = getspnam(name);
	if (NULL == shadow_pwd) {
		perror("getspnam()");
		return 1;
	}
	crypt_pwd = crypt(pwd, shadow_pwd->sp_pwdp);

	// 比对
	if (strcmp(crypt_pwd, shadow_pwd->sp_pwdp) == 0) {
		printf("登陆成功\n");
	} else {
		printf("密码错误\n");
	}

	return 0;
}

